#include <bits/stdc++.h>

#define pb push_back
#define eb emplace_back
#define fi first
#define se second
#define in read<int>()
#define lin read<ll>()
#define rep(i, x, y) for(int i = (x); i <= (y); i++)
#define per(i, x, y) for(int i = (x); i >= (y); i--)

using namespace std;

using ll = long long;
using db = double;
using pii = pair < int, int >;
using vec = vector < int >;
using veg = vector < pii >;

template < typename T > T read() {
	T x = 0; bool f = 0; char ch = getchar();
	while(!isdigit(ch)) f |= ch == '-', ch = getchar();
	while(isdigit(ch)) x = x * 10 + (ch ^ 48), ch = getchar();
	return f ? -x : x;
}

template < typename T > void chkmax(T &x, const T &y) { x = x > y ? x : y; }
template < typename T > void chkmin(T &x, const T &y) { x = x < y ? x : y; }

const int N = 65;
int sa, sb, sc, m, mod, n, ans;
int p[N], pot[N], tot, f[N << 1][N << 1][N << 1];
bool v[N];

ll qp(ll x, int t) { ll res = 1; for(; t; t >>= 1, x = x * x % mod) if(t & 1) res = res * x % mod; return res; }

void solve() {
	tot = 0; rep(i, 1, n) v[i] = 0;
	rep(i, 1, n) if(!v[i]) {
		int x = i, res = 0;
		while(!v[x]) v[x] = true, x = p[x], res++;
		pot[++tot] = res;
	} memset(f, 0, sizeof f);
	f[0][0][0] = 1;
	rep(i, 1, tot) {
		int x = pot[i];
		per(a, sa, 0)
			per(b, sb, 0)
			per(c, sc, 0) {
			(f[a + x][b][c] += f[a][b][c]) %= mod;
			(f[a][b + x][c] += f[a][b][c]) %= mod;
			(f[a][b][c + x] += f[a][b][c]) %= mod;
			f[a][b][c] = 0;
		}
	} (ans += f[sa][sb][sc]) %= mod;
}

int main() {
#ifndef ONLINE_JUDGE
	freopen("1.in", "r", stdin);
#endif
	sa = in, sb = in, sc = in; n = sa + sb + sc; m = in; mod = in;
	rep(i, 1, m) {
		rep(j, 1, n) p[j] = in;
		solve();
	} rep(j, 1, n) p[j] = j;
	solve(); ans = ans * qp(m + 1, mod - 2) % mod; printf("%d\n", ans);
	return 0;
}
